﻿Public Partial Class Shows
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' check for asyncPostBack
        If Page.IsPostBack = True Then

        Else

            ' populate shows
            PopulateShows()

        End If

    End Sub

    Public Sub LoadCharacters()
        PopulateCharacters(ddlShows.SelectedValue.ToLower)
    End Sub

    Private Sub DefineDataTable(ByRef dataTable As Data.DataTable)

        ' declare variables
        Dim dataColumn As Data.DataColumn

        ' instantiate
        dataColumn = New Data.DataColumn

        ' define data structure
        dataColumn.ColumnName = "ID"
        dataColumn.DataType = Type.GetType("System.Int32")
        dataColumn.AutoIncrement = True
        dataColumn.AutoIncrementSeed = 0
        dataColumn.AutoIncrementStep = 1

        ' add first column definition to datatable
        dataTable.Columns.Add(dataColumn)

        ' add second column definition to datatable
        dataTable.Columns.Add("Text", Type.GetType("System.String"))

        ' housekeeping
        dataColumn.Dispose()

    End Sub

    Private Sub PopulateDataRow(ByRef dataTable As Data.DataTable, ByVal text As String)

        ' declare variables
        Dim dataRow As Data.DataRow

        ' instantiate
        dataRow = dataTable.NewRow

        ' populate datarow
        dataRow("Text") = text

        ' add row to datatable
        dataTable.Rows.Add(dataRow)

    End Sub

    Private Function CreateListItem(ByVal value As Integer, ByVal text As String) As ListItem

        ' declare variables
        Dim listItem As ListItem

        ' instantiate
        listItem = New ListItem

        ' populate
        listItem.Value = value
        listItem.Text = text

        ' return listItem
        Return listItem

    End Function
    Private Sub PopulateDropDownList(ByRef dropDownList As DropDownList, ByVal id As Integer, ByVal text As String)

        ' populate
        dropDownList.Items.Add(CreateListItem(id, text))

    End Sub

    Private Sub PopulateShows()

        ' declare variables
        Dim dataTable As Data.DataTable
        Dim dataRow As Data.DataRow

        ' instantiate
        dataTable = New Data.DataTable

        ' define datatable
        DefineDataTable(dataTable)

        ' populate datarows
        PopulateDataRow(dataTable, "Please Select")
        PopulateDataRow(dataTable, "AirWolf")
        PopulateDataRow(dataTable, "KnightRider")
        PopulateDataRow(dataTable, "The A-Team")

        ' iterate
        For Each dataRow In dataTable.Rows

            PopulateDropDownList(ddlShows, dataRow.Item("ID"), dataRow.Item("Text"))

        Next

    End Sub

    Private Sub PopulateCharacters(ByVal show As String)

        ' declare variables
        Dim dataTable As Data.DataTable
        Dim dataRow As Data.DataRow

        ' instantiate
        dataTable = New Data.DataTable

        ' define datatable
        DefineDataTable(dataTable)

        ' add please select entry
        PopulateDataRow(dataTable, "Please Select")

        ' determine which characters to populate
        Select Case show

            Case "1"

                PopulateDataRow(dataTable, "Dominic Santini")
                PopulateDataRow(dataTable, "Stringfellow Hawke")

            Case "2"

                PopulateDataRow(dataTable, "Kit")
                PopulateDataRow(dataTable, "Michael Knight")

            Case "3"

                PopulateDataRow(dataTable, "B.A")
                PopulateDataRow(dataTable, "Face")
                PopulateDataRow(dataTable, "Hannibal")
                PopulateDataRow(dataTable, "Murdock")

        End Select

        ' clear existing items
        ddlCharacters.Items.Clear()

        ' iterate
        For Each dataRow In dataTable.Rows

            PopulateDropDownList(ddlCharacters, dataRow.Item("ID"), dataRow.Item("Text"))

        Next

    End Sub

    Protected Sub btntime_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btntime.Click
        lbltime.Text = Now.TimeOfDay.ToString
    End Sub
End Class